﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;

namespace Enring.Core
{
    /// <summary>
    /// 自动翻译对象
    /// </summary>
    public class Text
    {
        public string Value { get; set; }
        private string PropertyName { get; set; }
        private string ClassName { get; set; }
        public static implicit operator Text(string tstr)
        {
            Text t = new Text() { Value = tstr };
            string pName = string.Empty;
            var method = new StackFrame(1).GetMethod(); // 这里忽略1层堆栈，也就忽略了当前方法GetMethodName，这样拿到的就正好是外部调用GetMethodName的方法信息
            var property = (
                      from p in method.DeclaringType.GetProperties(
                               BindingFlags.Instance |
                               BindingFlags.Static |
                               BindingFlags.Public |
                               BindingFlags.NonPublic)
                      where p.GetGetMethod(true) == method || p.GetSetMethod(true) == method
                      select p).FirstOrDefault();
            if (property != null)
            {
                t.PropertyName = property.Name;
                t.ClassName = property.DeclaringType.Name;
            }
            return t;
        }
        public static implicit operator string(Text t)
        {
            if (t == null)
                return string.Empty;
            string defValue =  t.Value;
            //EnringContext.Current.CurrentUser.DefaultLanguage.Name    默认语言
            //t.ClassName   翻译字符分类
            //t.PropertyName    翻译字符子项
            return defValue;
        }
    }
}
